/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package estates;

//import estates.offer.*;
import estates.facade.*;
import estates.model.Estate;
import com.sun.webui.jsf.component.Checkbox;
import com.sun.webui.jsf.component.Tab;
import estates.validation.*;
import estates.opcje.*;
import estates.util.ParamGenerator;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import com.sun.webui.jsf.model.OptionsList;
//import com.sun.webui.jsf.model.
import estates.util.ErrorCodes;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.IntegerConverter;
import javax.faces.event.ValueChangeEvent;

import java.util.HashMap;
import javax.faces.validator.LengthValidator;
import javax.faces.validator.LongRangeValidator;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version Page1.java
 * @version Created on 2011-03-31, 14:53:08
 * @author Przemek
 */
public class Page1 extends AbstractPageBean {
  // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

  /**
   * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
   * This method is automatically generated, so any user-specified code inserted
   * here is subject to being replaced.</p>
   */
  private void _init() throws Exception {
    lengthValidator1.setMinimum(1);
    lengthValidator1.setMaximum(1000);
    longRangeValidator1.setMaximum(7);
    longRangeValidator2.setMaximum(3);
  }
  StringLengthValidator stringLengthValidator = new StringLengthValidator(150);

  public StringLengthValidator getStringLengthValidator() {
    return stringLengthValidator;
  }

  public void setStringLengthValidator(StringLengthValidator stringLengthValidator) {
    this.stringLengthValidator = stringLengthValidator;
  }
  private LengthValidator lengthValidator1 = new LengthValidator();
  private IntValidator intwalidator = new IntValidator();

  public IntValidator getIntwalidator() {
    return intwalidator;
  }

  public void setIntwalidator(IntValidator intwalidator) {
    this.intwalidator = intwalidator;
  }

  public LengthValidator getLengthValidator1() {
    return lengthValidator1;
  }

  public void setLengthValidator1(LengthValidator lv) {
    this.lengthValidator1 = lv;
  }
  private IntegerConverter integerConverter1 = new IntegerConverter();

  public IntegerConverter getIntegerConverter1() {
    return integerConverter1;
  }

  public void setIntegerConverter1(IntegerConverter ic) {
    this.integerConverter1 = ic;
  }
  private IntegerConverter integerConverter2 = new IntegerConverter();

  public IntegerConverter getIntegerConverter2() {
    return integerConverter2;
  }

  public void setIntegerConverter2(IntegerConverter ic) {
    this.integerConverter2 = ic;
  }
  private LongRangeValidator longRangeValidator1 = new LongRangeValidator();

  public LongRangeValidator getLongRangeValidator1() {
    return longRangeValidator1;
  }

  public void setLongRangeValidator1(LongRangeValidator lrv) {
    this.longRangeValidator1 = lrv;
  }
  private LongRangeValidator longRangeValidator2 = new LongRangeValidator();

  public LongRangeValidator getLongRangeValidator2() {
    return longRangeValidator2;
  }

  public void setLongRangeValidator2(LongRangeValidator lrv) {
    this.longRangeValidator2 = lrv;
  }
  private OptionsList new_userTypeDefaultOptions =
          new StaticOptionList(0, new String[]{"Zwykły", "Admin", "Pracownik", "Agent", "Firma"});

  public OptionsList getNew_userTypeDefaultOptions() {
    //  Option []tab = new Option[0];
    // new_userTypeDefaultOptions.setOptions(this.getApplicationBean1().getTypuzytkownika().toArray(tab));
    return new_userTypeDefaultOptions;
  }

  public void setNew_userTypeDefaultOptions(OptionsList ssol) {
    this.new_userTypeDefaultOptions = ssol;
  }
  private OptionsList new_typ_ofertyDefaultOptions =
          new StaticOptionList(1, new String[]{"Budynek", "Lokal"});

  public OptionsList getNew_typ_ofertyDefaultOptions() {
    //     Option []tab = new Option[0];
    //   new_typ_ofertyDefaultOptions.setOptions(this.getApplicationBean1().getTypoferty().toArray(tab));

    return new_typ_ofertyDefaultOptions;
  }

  public void setNew_typ_ofertyDefaultOptions(OptionsList ssol) {
    this.new_typ_ofertyDefaultOptions = ssol;
  }
  private OptionsList new_stan_budynkuDefaultOptions =
          new StaticOptionList(0, new String[]{"Nie wybrano", "Punktowiec", "Klatkowiec", "Korytarzowiec",
            "Galeriowiec", "Szeregowa", "Bliźniacza", "Wolnostojąca"});

  public OptionsList getNew_stan_budynkuDefaultOptions() {
    return new_stan_budynkuDefaultOptions;
  }

  public void setNew_stan_budynkuDefaultOptions(OptionsList ssol) {
    this.new_stan_budynkuDefaultOptions = ssol;
  }
  private OptionsList new_typ_zabudowyDefaultOptions =
          new StaticOptionList(0, new String[]{"Nie wybrano", "Punktowiec", "Klatkowiec", "Korytarzowiec",
            "Galeriowiec", "Szeregowa", "Bliźniacza", "Wolnostojąca"});

  public OptionsList getNew_typ_zabudowyDefaultOptions() {

    return new_typ_zabudowyDefaultOptions;
  }

  public void setNew_typ_zabudowyDefaultOptions(OptionsList ssol) {
    this.new_typ_zabudowyDefaultOptions = ssol;
  }
  private SingleSelectOptionsList new_typ_lokaluDefaultOptions = new SingleSelectOptionsList();

  public SingleSelectOptionsList getNew_typ_lokaluDefaultOptions() {
    //   Option []tab = new Option[0];
    // new_typ_lokaluDefaultOptions.setOptions(this.getApplicationBean1().getTyplokalu().toArray(tab));
    return new_typ_lokaluDefaultOptions;
  }

  public void setNew_typ_lokaluDefaultOptions(SingleSelectOptionsList ssol) {

    this.new_typ_lokaluDefaultOptions = ssol;
  }
  private TextField new_cena = new TextField();

  public TextField getNew_cena() {
    return new_cena;
  }

  public void setNew_cena(TextField tf) {
    this.new_cena = tf;
  }
  private TextField new_powierzchnia = new TextField();

  public TextField getNew_powierzchnia() {
    return new_powierzchnia;
  }

  public void setNew_powierzchnia(TextField tf) {
    this.new_powierzchnia = tf;
  }
  private TextField new_wojewodztwo = new TextField();

  public TextField getNew_wojewodztwo() {
    return new_wojewodztwo;
  }

  public void setNew_wojewodztwo(TextField tf) {
    this.new_wojewodztwo = tf;
  }
  private TextField new_miejscowosc = new TextField();

  public TextField getNew_miejscowosc() {
    return new_miejscowosc;
  }

  public void setNew_miejscowosc(TextField tf) {
    this.new_miejscowosc = tf;
  }
  private TextArea new_opis = new TextArea();

  public TextArea getNew_opis() {
    return new_opis;
  }

  public void setNew_opis(TextArea ta) {
    this.new_opis = ta;
  }
  private TextField new_rok_budowy = new TextField();

  public TextField getNew_rok_budowy() {
    return new_rok_budowy;
  }

  public void setNew_rok_budowy(TextField tf) {
    this.new_rok_budowy = tf;
  }
  private TextField new_ulica = new TextField();

  public TextField getNew_ulica() {
    return new_ulica;
  }

  public void setNew_ulica(TextField tf) {
    this.new_ulica = tf;
  }
  private TextField new_stan_prawny = new TextField();

  public TextField getNew_stan_prawny() {
    return new_stan_prawny;
  }

  public void setNew_stan_prawny(TextField tf) {
    this.new_stan_prawny = tf;
  }
  private TextField new_liczba_pieter = new TextField();

  public TextField getNew_liczba_pieter() {
    return new_liczba_pieter;
  }

  public void setNew_liczba_pieter(TextField tf) {
    this.new_liczba_pieter = tf;
  }
  private TextField new_numer_budynku = new TextField();

  public TextField getNew_numer_budynku() {
    return new_numer_budynku;
  }

  public void setNew_numer_budynku(TextField tf) {
    this.new_numer_budynku = tf;
  }
  private TextField new_kod_pocztowy = new TextField();

  public TextField getNew_kod_pocztowy() {
    return new_kod_pocztowy;
  }

  public void setNew_kod_pocztowy(TextField tf) {
    this.new_kod_pocztowy = tf;
  }
  private TextField new_liczba_pokoi = new TextField();

  public TextField getNew_liczba_pokoi() {
    return new_liczba_pokoi;
  }

  public void setNew_liczba_pokoi(TextField tf) {
    this.new_liczba_pokoi = tf;
  }
  private DropDown new_typ_lokalu = new DropDown();

  public DropDown getNew_typ_lokalu() {
    return new_typ_lokalu;
  }

  public void setNew_typ_lokalu(DropDown dd) {
    this.new_typ_lokalu = dd;
  }
  private TextField new_pietro = new TextField();

  public TextField getNew_pietro() {
    return new_pietro;
  }

  public void setNew_pietro(TextField tf) {
    this.new_pietro = tf;
  }
  private TextField new_numer_lokalu = new TextField();

  public TextField getNew_numer_lokalu() {
    return new_numer_lokalu;
  }

  public void setNew_numer_lokalu(TextField tf) {
    this.new_numer_lokalu = tf;
  }
  private TextField new_liczba_lazieniek = new TextField();

  public TextField getNew_liczba_lazieniek() {
    return new_liczba_lazieniek;
  }

  public void setNew_liczba_lazieniek(TextField tf) {
    this.new_liczba_lazieniek = tf;
  }
  private Checkbox new_czy_rozkladowy = new Checkbox();

  public Checkbox getNew_czy_rozkladowy() {
    return new_czy_rozkladowy;
  }

  public void setNew_czy_rozkladowy(Checkbox c) {
    this.new_czy_rozkladowy = c;
  }
  private Tab tab4 = new Tab();

  public Tab getTab4() {
    return tab4;
  }

  public void setTab4(Tab t) {
    this.tab4 = t;
  }
  private DropDown new_typ_oferty1 = new DropDown();

  public DropDown getNew_typ_oferty1() {
    return new_typ_oferty1;
  }

  public void setNew_typ_oferty1(DropDown dd) {
    this.new_typ_oferty1 = dd;
  }
  private Tab tab2 = new Tab();

  public Tab getTab2() {
    return tab2;
  }

  public void setTab2(Tab t) {
    this.tab2 = t;
  }
  private Tab tab3 = new Tab();

  public Tab getTab3() {
    return tab3;
  }

  public void setTab3(Tab t) {
    this.tab3 = t;
  }
  private DropDown dropDown1 = new DropDown();

  public DropDown getDropDown1() {
    return dropDown1;
  }

  public void setDropDown1(DropDown dd) {
    this.dropDown1 = dd;
  }

  // </editor-fold>
  /**
   * <p>Construct a new Page bean instance.</p>
   */
  public Page1() {
  }

  /**
   * <p>Callback method that is called whenever a page is navigated to,
   * either directly via a URL, or indirectly via page navigation.
   * Customize this method to acquire resources that will be needed
   * for event handlers and lifecycle methods, whether or not this
   * page is performing post back processing.</p>
   *
   * <p>Note that, if the current request is a postback, the property
   * values of the components do <strong>not</strong> represent any
   * values submitted with this request.  Instead, they represent the
   * property values that were saved for this view when it was rendered.</p>
   */
  @Override
  public void init() {



    // Perform initializations inherited from our superclass
    super.init();


    //ddZabudowa.setItems(zabudowa);


    //ddStan.setItems(stan);
    // Perform application initialization that must complete
    // *before* managed components are initialized
    // TODO - add your own initialiation code here

    // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
    // Initialize automatically managed components
    // *Note* - this logic should NOT be modified
    try {
      _init();
    } catch (Exception e) {
      log("Page1 Initialization Failure", e);
      throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
    }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here

  }

  /**
   * <p>Callback method that is called after the component tree has been
   * restored, but before any event processing takes place.  This method
   * will <strong>only</strong> be called on a postback request that
   * is processing a form submit.  Customize this method to allocate
   * resources that will be required in your event handlers.</p>
   */
  @Override
  public void preprocess() {
  }

  /**
   * <p>Callback method that is called just before rendering takes place.
   * This method will <strong>only</strong> be called for the page that
   * will actually be rendered (and not, for example, on a page that
   * handled a postback and then navigated to a different page).  Customize
   * this method to allocate resources that will be required for rendering
   * this page.</p>
   */
  @Override
  public void prerender() {
  }

  /**
   * <p>Callback method that is called after rendering is completed for
   * this request, if <code>init()</code> was called (regardless of whether
   * or not this was the page that was actually rendered).  Customize this
   * method to release resources acquired in the <code>init()</code>,
   * <code>preprocess()</code>, or <code>prerender()</code> methods (or
   * acquired during execution of an event handler).</p>
   */
  @Override
  public void destroy() {
  }

  /**
   * <p>Return a reference to the scoped data bean.</p>
   *
   * @return reference to the scoped data bean
   */
  protected SessionBean1 getSessionBean1() {
    return (SessionBean1) getBean("SessionBean1");
  }

  /**
   * <p>Return a reference to the scoped data bean.</p>
   *
   * @return reference to the scoped data bean
   */
  protected RequestBean1 getRequestBean1() {
    return (RequestBean1) getBean("RequestBean1");
  }

  /**
   * <p>Return a reference to the scoped data bean.</p>
   *
   * @return reference to the scoped data bean
   */
  protected ApplicationBean1 getApplicationBean1() {
    return (ApplicationBean1) getBean("ApplicationBean1");
  }

  public String bdodaj_action() {
    Fasada1 e = this.getApplicationBean1().getFasada();


    HashMap<String, Object> arg1 = new HashMap();
    HashMap<String, Object> arg2 = new HashMap();

    Integer typOferty = ParamGenerator.ConvertSelection(new_typ_oferty1);

    /* Budowa HashMapy tworzenia oferty */

    //pola tekstowe
    String[] txtFieldsOferta = {"wojewodztwo", "miejscowosc", "opis"};
    TextField[] txtComponentsOferta = {new_wojewodztwo, new_miejscowosc, new_opis};

    String[] txtFieldsBudynek = {"stan_prawny", "ulica"};
    TextField[] txtComponentsBudynek = {new_stan_prawny, new_ulica};

    /*String [] txtFieldsLokal = {"wojewodztwo","miejscowosc","opis","stan_prawny",
    "dzielnica","ulica","forma_wlasnosci"};
    TextField [] txtComponentsLokal = {new_wojewodztwo,new_miejscowosc,new_opis,
    new_stan_prawny,new_dzielnica,new_ulica,
    new_forma_wlasnosci};
     */

    //pola liczbowe
    String[] intFieldsOferta = {"cena", "powierzchnia"};
    TextField[] intComponentsOferta = {new_cena, new_powierzchnia};

    String[] intFieldsBudynek = {"rok_budowy", "liczba_pieter", "liczba_pokoi",
      "kod_pocztowy", "numer_budynku"};
    TextField[] intComponentsBudynek = {new_rok_budowy,
      new_liczba_pieter, new_liczba_pokoi,
      new_kod_pocztowy, new_numer_budynku};

    String[] intFieldsLokal = {"liczba_lazieniek",
      "pietro", "numer_lokalu"};
    TextField[] intComponentsLokal = {new_liczba_lazieniek, new_pietro, new_numer_lokalu};


    //komponenty typu "Dropdown"
    String[] selectionsLokal = {"typ_lokalu",};
    DropDown[] selComponentsLokal = {new_typ_lokalu};

    //parsowanie dla OFERTY
    arg1.put("typ_oferty", typOferty);
    ParamGenerator.parseStrFields(arg1, txtFieldsOferta, txtComponentsOferta, false);
    ParamGenerator.parseIntFields(arg1, intFieldsOferta, intComponentsOferta, false);

    if (typOferty >= 1) {//BUDYNEK
      ParamGenerator.parseStrFields(arg1, txtFieldsBudynek, txtComponentsBudynek, false);
      ParamGenerator.parseIntFields(arg1, intFieldsBudynek, intComponentsBudynek, false);
    }
    if (typOferty == 2) {//LOKAL
      //ParamGenerator.parseStrFields(arg1, txtFieldsLokal, txtComponentsLokal);
      ParamGenerator.parseIntFields(arg1, intFieldsLokal, intComponentsLokal, false);
      ParamGenerator.parseSelection(arg1, selectionsLokal, selComponentsLokal, false);

      //pole checkbox
      arg1.put("czy_rozkladowy", (Boolean) (new_czy_rozkladowy.isChecked() ? true : false));
    }

    //wybrany user
    arg2.put("nip", this.dropDown1.getValue().toString());
    int result = this.getApplicationBean1().dodajOferte(arg1, arg2);

    //TODO wykorzystać obiekt transferowy - kod bledu
    if (result >= 0) {
      this.getRequestBean1().setKomunikat("Pomyślnie dodano ofertę o identyfikatorze " + result);
      this.getRequestBean1().setNapis("Przejdź do listy ofert");
      this.getSessionBean1().setLink("listaofert");
      return "sukces";
    } else {
      this.getRequestBean1().setKomunikat("Utworzenie oferty zakończone niepowodzeniem");
      return "blad";
    }
  }

  private void fixTabs(String selected) {
    if (selected.equals("1")) {
        this.getRequestBean1().setTabVisibility(RequestBean1.VIS_SHOWDEFAULT);
    } else if (selected.equals("2")) {
        this.getRequestBean1().setTabVisibility(RequestBean1.VIS_SHOWALL);
    }
  }

  public void new_typ_oferty1_processValueChange(ValueChangeEvent event) {
    String selected = event.getNewValue().toString();
    //tymczasowa implementacja
    fixTabs(selected);
  }

  public String tab2_action() {
    // TODO: Process the action. Return value is a navigation
    // case name where null will return to the same page.
    fixTabs(new_typ_oferty1.getValue().toString());
    return null;
  }

  public void new_typ_oferty2_validate(FacesContext context, UIComponent component, Object value) {
    fixTabs(value.toString());
  }
}

